home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_03_01 / 3n01047a < prev    next >
Text File  |  1991-10-15  |  6KB  |  174 lines

  1. { LISTING 2 }
  2. {$M 2048,1024,R-,S-,D-,L-,N-,A-}
  3. program Bye_With_Owl;
  4.  
  5.    (* BYE2.PAS - Quick Exit For Windows - In Object Windows Library
  6.     * Written by Richard R. Sands in Turbo Pascal for Windows
  7.     *)
  8.  
  9. USES
  10.    WObjects, WinTypes, WinProcs;
  11.  
  12. {$R Bye2.Res}
  13.  
  14. CONST
  15.    AppName = 'Bye2';
  16.    IniAppName = 'Bye';  { As listed in WIN.INI }
  17.  
  18.    idm_Quit    = 100;  { Sys Message Command: Quit Windows }
  19.    idm_Confirm = 101;  { Sys Message Command: Confirm Exit }
  20.    idm_OS      = 102;  { Sys Message Command: Execute DOS Shell }
  21.    idm_About   = 103;  { Sys Message Command: Show About Dialog }
  22.  
  23. type
  24.   { Define a TApplication descendant }
  25.   tWExitApp = object(tApplication)
  26.     procedure InitMainWindow; virtual;
  27.   end;
  28.  
  29.   pByeWin = ^tByeWin;
  30.   tByeWin = object(tWindow)
  31.      Confirm: Boolean;  { ask before quitting? }
  32.  
  33.      { Window/Object Setup }
  34.      constructor Init(AParent: pWindowsObject; ATitle: pChar);
  35.      procedure   GetWindowClass(var AWndClass : tWndClass); virtual;
  36.      procedure   SetUpWindow; virtual;
  37.  
  38.      { Window Message Methods }
  39.      procedure   wmSysCommand(var Msg:tMessage); virtual wm_First + wm_SysCommand;
  40.      procedure   wmQueryOpen(var Msg:tMessage); virtual wm_First + wm_QueryOpen;
  41.  
  42.      { Private Routines }
  43.      procedure   ToggleConfirm;
  44.      procedure   SetConfirmState;
  45.      procedure   Quit;
  46.   end;
  47.  
  48. { ------------------------------------------------------------------------- }
  49. { tByeWin                                                                   }
  50. { ------------------------------------------------------------------------- }
  51. constructor tByeWin.Init(AParent: pWindowsObject; ATitle: PChar);
  52.  
  53.   { Construct the tByeWin's object. }
  54.  
  55.   begin
  56.      Confirm := FALSE;  { Defaults to No Confirm }
  57.      tWindow.Init(AParent, Atitle);
  58.      Attr.Style := ws_Overlapped OR ws_MinimizeBox OR ws_SysMenu
  59.   end;
  60.  
  61. { ------------------------------------------------------------------------- }
  62. procedure tByeWin.GetWindowClass(var AWndClass : TWndClass);
  63.   begin
  64.     tWindow.GetWindowClass(AWndClass);
  65.     AWndClass.hIcon := LoadIcon(hInstance, 'ICON')  { Assign the icon }
  66.   end;
  67.  
  68. { ------------------------------------------------------------------------- }
  69. procedure tByeWin.SetUpWindow;
  70.   var SysMenu : hMenu;
  71.       Buffer : Array[0..1] of char;
  72.   begin
  73.      SysMenu := GetSystemMenu(hWindow, FALSE);
  74.  
  75.      { Add Items to the System Menu }
  76.      AppendMenu(SysMenu, mf_SysMenu + mf_Separator, 0, NIL);
  77.      AppendMenu(SysMenu, mf_SysMenu, idm_Quit,    '&Exit Windows');
  78.      AppendMenu(SysMenu, mf_SysMenu, idm_Confirm, 'Confirm &Shutdown');
  79.      AppendMenu(SysMenu, mf_SysMenu, idm_OS,      '&Operating System');
  80.      AppendMenu(SysMenu, mf_SysMenu, idm_About,   '&About Bye...');
  81.  
  82.      { Now get the state of the Confirm Option - This is in WIN.INI }
  83.      GetProfileString(IniAppName, 'Confirm', 'N', Buffer, sizeof(Buffer));
  84.      if Buffer[0] = 'Y' then
  85.         ToggleConfirm
  86.   end;
  87.  
  88. { ------------------------------------------------------------------------- }
  89. procedure tByeWin.wmSysCommand(var Msg:tMessage);
  90.  
  91.   { Handle the System Menu Items that were installed }
  92.  
  93.   var About: pDialog;
  94.   begin
  95.       case Msg.wParam of
  96.         idm_About  : begin
  97.                         New(About, Init(@Self, 'AboutBox'));
  98.                         Application^.ExecDialog(About);
  99.                      end;
  100.         idm_Confirm: ToggleConfirm;
  101.         idm_OS     : WinExec('Command.Com', sw_Normal);
  102.         idm_Quit   : Quit
  103.       else
  104.         DefWndProc(Msg)
  105.       end
  106.   end;
  107.  
  108. { ------------------------------------------------------------------------- }
  109. Procedure tByeWin.wmQueryOpen(var Msg:tMessage);
  110.   begin
  111.       Msg.Result := 0;  { If this returns 0 then we cannot restore our window }
  112.       Quit
  113.   end;
  114.  
  115. { ------------------------------------------------------------------------- }
  116. Procedure tByeWin.Quit;
  117.   begin
  118.       if Confirm then
  119.       begin
  120.           MessageBeep(0);
  121.           if MessageBox(hWindow, 'Are you sure you want to exit Windows?', 'Exit Windows?',
  122.                         mb_IconQuestion + mb_OkCancel) = id_Cancel then
  123.               EXIT
  124.       end;
  125.       SetConfirmState;   { Write our Confirm=Y/N entry in WIN.INI }
  126.       ExitWindows(0, 0)  { See 'ya }
  127.   end;
  128.  
  129. { ------------------------------------------------------------------------- }
  130. Procedure tByeWin.ToggleConfirm;
  131.   var SysMenu  : hMenu;
  132.   begin
  133.       SysMenu := GetSystemMenu(hWindow, FALSE);
  134.       Confirm := NOT Confirm;
  135.       if Confirm then
  136.          CheckMenuItem(SysMenu, idm_Confirm, mf_byCommand+mf_Checked)
  137.       else
  138.          CheckMenuItem(SysMenu, idm_Confirm, mf_byCommand+mf_UnChecked)
  139.   end;
  140.  
  141. { -------------------------------------------------------------------------- }
  142. Procedure tByeWin.SetConfirmState;
  143.   { Sets the [BYE]
  144.              Confirm=Y/N
  145.      WIN.INI setting }
  146.   var Buffer: Array[0..1] of char;
  147.   begin
  148.      if Confirm then Buffer[0] := 'Y'
  149.      else
  150.         Buffer[0] := 'N';
  151.      Buffer[1] := #0;  { Null terminate the String }
  152.      WriteProfileString(IniAppName, 'Confirm', Buffer)
  153.   end;
  154.  
  155. { ------------------------------------------------------------------------- }
  156. { tWExitApp                                                                 }
  157. { ------------------------------------------------------------------------- }
  158. procedure tWExitApp.InitMainWindow;
  159.  
  160.   { Construct the TWExitApp's MainWindow object: A tByeWin }
  161.  
  162.   begin
  163.      MainWindow := New(pByeWin, Init(nil, AppName))
  164.   end;
  165.  
  166. { ------------------------------------------------------------------------- }
  167. var WExitApp: tWExitApp;
  168. begin
  169.   WExitApp.Init(AppName);
  170.   ShowWindow(WExitApp.MainWindow^.hWindow, sw_ShowMinimized); { Minimize Window }
  171.   WExitApp.Run;
  172.   WExitApp.Done
  173. end.
  174.